package com.zebone.mongodb;

import java.util.Date;
import java.util.List;

import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Morphia;
import org.mongodb.morphia.query.FindOptions;
import org.mongodb.morphia.query.Query;

import com.mongodb.MongoClient;
import com.zebone.morphia.entity.User;

public class TestUserQuery {
	
	public void testQueryName(){
		Morphia morphia = new Morphia();

		// tell Morphia where to find your classes
		// can be called multiple times with different packages or classes
		morphia.mapPackage("com.zebone.morphia.entity");

		// create the Datastore connecting to the default port on the local host
		Datastore datastore = morphia.createDatastore(new MongoClient(), "morphia_example");
		Query<User> query = datastore.createQuery(User.class);
		long beginTime = new Date().getTime();
		query.filter("name =","cz31000200");
		List<User> userList = query.asList();
		for(User user:userList){
			System.out.println("####################"+user.getName()+"####################"+user.getAge());
		}
		long endTime = new Date().getTime();
		System.out.println(" execute time :" + (endTime - beginTime));
	}
	
	public void testQueryPage(){
		Morphia morphia = new Morphia();

		// tell Morphia where to find your classes
		// can be called multiple times with different packages or classes
		morphia.mapPackage("com.zebone.morphia.entity");

		// create the Datastore connecting to the default port on the local host
		Datastore datastore = morphia.createDatastore(new MongoClient(), "morphia_example");
		
		Query<User> query = datastore.createQuery(User.class);
		List<User> userList = query.asList(new FindOptions().skip(1).limit(10));
		for(User user:userList){
			System.out.println("####################"+user.getName()+"####################"+user.getAge());
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
	      //new TestUserQuery().testQueryPage();
	      new TestUserQuery().testQueryName();
	}

}
